;;;;;; binary-io.scm - Binary Input/Output module.    -*- Mode: Scheme -*-
;;;;;; Author: Erik Silkensen <silkense@colorado.edu>
;;;;;; Version: 31 May 2009

#lang scheme

;;; This module provides procedures for binary i/o operations. Numbers
;;; are written in big-endian format, following the Java(TM) Virtual
;;; Machine.

(provide (all-defined-out))

(define (write-short short [out (current-output-port)])
  (write-bytes (integer->integer-bytes short 2 (< short 0) #t) out))

(define (write-int int [out (current-output-port)])
  (write-bytes (integer->integer-bytes int 4 (< int 0) #t) out))

(define (write-float float [out (current-output-port)])
  (write-bytes (real->floating-point-bytes float 4 #t) out))

(define (write-double double [out (current-output-port)])
  (write-bytes (real->floating-point-bytes double 8 #t) out))

(define (write-long long [out (current-output-port)])
  (write-bytes (integer->integer-bytes long 8 (< long 0) #t) out))

(define (write-utf string [out (current-output-port)])
  (write-short (string-length string) out)
  (write-bytes (string->bytes/utf-8 string) out))
